import logging
import os
import sys
from utils import get_current_time_str

global_time_str = get_current_time_str()


class Logger:
    def __init__(self, filename='COMMRC', filepath='./logger'):
        # 如果不存在就创建目录
        os.makedirs(filepath, exist_ok=True)

        # 生成 logger 对象
        logger = logging.getLogger('')
        logger.setLevel(logging.DEBUG)

        # 添加一个 file handler
        file_handler = logging.FileHandler(f'{filepath}/{filename}.log')
        fmt_str = '[%(asctime)s] %(levelname)s [%(filename)s.%(funcName)s:%(lineno)d] %(message)s'
        formatter = logging.Formatter(fmt_str, datefmt='%a, %d %b %Y %H:%M:%S')
        file_handler.setFormatter(formatter)
        logger.addHandler(file_handler)

        # 添加一个 stream handler
        stream_handler = logging.StreamHandler(sys.stdout)
        logger.addHandler(stream_handler)
        self.logger = logger
        pass

    pass
